/**
 * Created by liubin on 2017/5/10.
 */



var DzpkTableChatLayer = GameCCSBase.extend({
    Button_chat : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DzpkTableChat.json");

        //button
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.setDialog(false);
            }
        }, this);
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.setDialog(false);
            }
        }, this);

        this.loadText();

        this.loadFace();

        this.setVisible(false);
    },

    onEnter : function(){
        this._super();
    },

    onExit : function(){
        this._super();
    },

    setDialog : function(isVisible){
        if (isVisible){
            this.setVisible(true);
            this.showAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
        }else{
            this.closeAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
        }
    },

    sendChatMsg : function(index){
        mo.gameMsgManager.sendUserChatMsg(mo.gameUserInfo.uid, index.toString(), 1);

        if (this.Button_chat != null){
            this.Button_chat.stopAllActions();
            this.Button_chat.setEnabled(false);
            var that = this;
            this.Button_chat.runAction(cc.sequence(
                cc.delayTime(3.5),
                cc.callFunc(function(){
                    that.Button_chat.setEnabled(true);
                })
            ));
        }
    },

    loadText : function(){
        var that = this;
        for (var i = 0; i < 13; i++){
            var Image_textSelect = mo.seekWidgetByName(this.ccsNode, "Image_textSelect" + i);
            mo.gameHelper.setRootTitleText(Image_textSelect, mo.gameUserInfo.userCustomText[i]);
            Image_textSelect.setTag(i);

            Image_textSelect.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)){
                    that.setTextSelect(sender, false);

                    //发送
                    var index = sender.getTag();
                    cc.log("index:" + index);
                    that.sendChatMsg(index);
                    this.setDialog(false);
                }else if (type == ccui.Widget.TOUCH_BEGAN){
                    that.setTextSelect(sender, true);
                }else if (type == ccui.Widget.TOUCH_CANCELED){
                    that.setTextSelect(sender, false);
                }
            }, this);
        }
    },

    setTextSelect : function(root, isSelect){
        var Text_text = mo.seekWidgetByName(root, "Text_text");
        Text_text.setTextColor(!isSelect ? cc.color(91, 142, 172) : cc.color(255, 255, 255));
    },

    loadFace : function(){
        var that = this;
        var rootSize = cc.size(300, Math.ceil(27 / 4) * 80);
        var itemSize = cc.size(100, 80);
        var ScrollView_face = mo.seekWidgetByName(this.ccsNode, "ScrollView_face");
        ScrollView_face.setInnerContainerSize(rootSize);

        for (var i = 0; i < 27; i++){
            var Image_image = mo.seekWidgetByName(this.ccsNode, "Image_image" + i);
            Image_image.loadTexture("emoji_index_" + i + ".png", ccui.Widget.PLIST_TEXTURE);
            Image_image.setTag(i);

            //排序
            var x = itemSize.width / 2 + i % 4 * itemSize.width;
            var y = rootSize.height - itemSize.height / 2 - parseInt(i / 4) * itemSize.height;
            Image_image.setPosition(cc.p(x, y));

            //处理
            Image_image.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)){
                    that.setFaceSelect(sender, false);

                    //发送
                    var index = sender.getTag();
                    cc.log("index:" + index);
                    that.sendChatMsg(index + 51);
                    this.setDialog(false);
                }else if (type == ccui.Widget.TOUCH_BEGAN){
                    that.setFaceSelect(sender, true);
                }else if (type == ccui.Widget.TOUCH_CANCELED){
                    that.setFaceSelect(sender, false);
                }
            }, this);
        }
    },

    setFaceSelect : function(root, isSelect){
        root.setScale(isSelect ? 1 : 0.8);
    }
});